package com.haois.mp.wechat.config;

import com.haois.mp.wechat.constants.Enum.MenuEnum;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.bean.menu.WxMenu;
import me.chanjar.weixin.common.bean.menu.WxMenuButton;
import me.chanjar.weixin.mp.api.WxMpMenuService;
import me.chanjar.weixin.mp.api.WxMpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

/**
 * 菜单配置
 */
@Component
public class MenuConfig {

    @Autowired
    private WxMpService wxMpService;

    @Value("${defined.wxmp.base-url}")
    private String baseUrl;


    @Autowired
    private WxMpMenuService wxMpMenuService;

    @PostConstruct
    public void afterPropertiesSet() throws Exception {
        WxMenu wxMenu = this.createWxMenu();
//        wxMpMenuService.menuCreate(wxMenu);
    }

    /**
     * 创建微信菜单
     *
     * @return
     */
    public WxMenu createWxMenu() throws UnsupportedEncodingException {

        /** ====================== 菜单一 ============================= **/
        //1 我的
        WxMenuButton btn1 = new WxMenuButton();
        btn1.setKey(MenuEnum.BTN_INFO_SERVICE.getKey());
        btn1.setName(MenuEnum.BTN_INFO_SERVICE.getName());
        List<WxMenuButton> menu1 = new ArrayList<WxMenuButton>();
        btn1.setSubButtons(menu1);

        menu1.add(newVIEWButton(MenuEnum.BTN_DRIVER_LINK));
        menu1.add(newVIEWButton(MenuEnum.BTN_REMIND_SERVER));
        menu1.add(newClickButton(MenuEnum.BTN_INFO_MORE));
        menu1.add(newVIEWButton(MenuEnum.BTN_INFO_MINE));

        /** ====================== 菜单二 ============================= **/
        //2 团队
        WxMenuButton btn2 = new WxMenuButton();
        btn2.setKey(MenuEnum.BTN_GROUP_SERVICE.getKey());
        btn2.setName(MenuEnum.BTN_GROUP_SERVICE.getName());
        List<WxMenuButton> menu2 = new ArrayList<WxMenuButton>();
        btn2.setSubButtons(menu2);
        menu2.add(newVIEWButton(MenuEnum.BTN_GROUP_MOBILE));
        menu2.add(newVIEWButton(MenuEnum.BTN_GROUP_NOTICE));
        menu2.add(newClickButton(MenuEnum.BTN_INFO_QUICK));
        menu2.add(newVIEWButton(MenuEnum.BTN_GROUP_JOIN));


        /** ====================== 菜单三 ============================= **/
        // 3 程序
        WxMenuButton btn3 = new WxMenuButton();
        btn3.setKey(MenuEnum.BTN_APP_SERVICE.getKey());
        btn3.setName(MenuEnum.BTN_APP_SERVICE.getName());
        List<WxMenuButton> menu3 = new ArrayList<WxMenuButton>();
        btn3.setSubButtons(menu3);

        menu3.add(newVIEWButton(MenuEnum.BTN_INFO_WECHAT));
        menu3.add(newClickButton(MenuEnum.BTN_CONTENT_ME));
        menu3.add(newOtherButton(MenuEnum.BTN_INFO_SCAN, WxConsts.MenuButtonType.SCANCODE_WAITMSG));
        menu3.add(newVIEWButton(MenuEnum.BTN_YELLOW_PAGES));

        /** ====================== 总菜单 ============================= **/

        List<WxMenuButton> btns = new ArrayList<WxMenuButton>();
        btns.add(btn1);
        btns.add(btn2);
        btns.add(btn3);

        WxMenu wxMenu = new WxMenu();
        wxMenu.setButtons(btns);
        return wxMenu;
    }


    public WxMenuButton newVIEWButton(MenuEnum menuEnum) {
        WxMenuButton wxMenuButton = new WxMenuButton();
        wxMenuButton.setKey(menuEnum.getKey());
        wxMenuButton.setName(menuEnum.getName());
        wxMenuButton.setType(WxConsts.MenuButtonType.VIEW);
        String url = baseUrl + menuEnum.getUrl();
        url = wxMpService.oauth2buildAuthorizationUrl(url,
                WxConsts.OAuth2Scope.SNSAPI_BASE, menuEnum.getKey());
        wxMenuButton.setUrl(url);
        return wxMenuButton;
    }

    public WxMenuButton newClickButton(MenuEnum menuEnum) {
        WxMenuButton wxMenuButton = new WxMenuButton();
        wxMenuButton.setKey(menuEnum.getKey());
        wxMenuButton.setName(menuEnum.getName());
        wxMenuButton.setType(WxConsts.MenuButtonType.CLICK);
        return wxMenuButton;
    }

    public WxMenuButton newOtherButton(MenuEnum menuEnum, String type) {
        WxMenuButton wxMenuButton = new WxMenuButton();
        wxMenuButton.setKey(menuEnum.getKey());
        wxMenuButton.setName(menuEnum.getName());
        wxMenuButton.setType(type);
        String url = baseUrl + menuEnum.getUrl();
        url = wxMpService.oauth2buildAuthorizationUrl(url,
                WxConsts.OAuth2Scope.SNSAPI_BASE, menuEnum.getKey());
        wxMenuButton.setUrl(url);
        return wxMenuButton;
    }
}
